const express = require('express')
const faker = require('faker')
const assert = require('http-assert')
const Facility = require('../db/model/facility')
const Log = require('../db/model/log')
const { SuccessModel } = require('../utils/resModel')
const router = express.Router()

/**
 * 分页获取所有设施
 */
router.post('/facility', async (req, res) => {
  const { page = 1, size = 0 } = req.body
  const facility = await Facility.find()
    .select({ logs: false })
    .skip((page - 1) * size)
    .limit(size)
  assert(facility, 404, '获取设施失败')
  res.json(new SuccessModel(facility, '获取设施成功'))
})

/**
 * 获取某个设施下所有的日志
 */
router.get('/facility/:id', async (req, res) => {
  const id = req.params.id
  const logs = await Facility.findById(id).populate('logs')
  assert(logs, 404, '获取当前设施下所有日志失败')
  res.json(new SuccessModel(logs, '获取设施下所有日志成功'))
})

/**
 * 添加某个设施
 */
router.post('/facility', async (req, res) => {
  const facility = new Facility(req.body)
  const info = await facility.save()
  assert(info, 400, '添加设施失败')
  res.json(new SuccessModel(info, '添加设施成功'))
})

/**
 * 修改某个设施, 只能修改名字，经纬度，状态，类型
 */
router.put('/facility/:id', async (req, res) => {
  const id = req.params.id
  const facility = req.body
  const info = await Facility.findByIdAndUpdate(id, facility)
  assert(info, 400, '当前设施修改失败')
  res.json(new SuccessModel(info, '修改信息成功'))
})

/**
 * 删除某个设施
 */
router.delete('/facility/:id', async (req, res) => {
  const id = req.params.id
  const info = await Facility.findByIdAndDelete(id)
  assert(info, 400, '删除设施失败')
  res.json(new SuccessModel(info, '删除设施成功'))
})

/**
 * 分页获取所有日志记录
 */
router.post('/log', async (req, res) => {
  const { page = 1, size = 10 } = req.body
  const logs = await Log.find()
    .skip((page - 1) * size)
    .limit(size)
  assert(logs, 404, '获取所有日志失败')
  res.json(new SuccessModel(logs, '获取日志信息成功'))
})

/**
 * 日志的创建
 */
router.post('/log', async (req, res) => {
  // 根据传入的设施id添加日志
  const { facilityId, logData } = req.body
  const log = new Log(logData)
  const info = await Facility.findByIdAndUpdate(facilityId, {
    $push: {
      logs: log
    }
  })
  await log.save()
  assert(info, 400, '添加日志失败')
  res.json(new SuccessModel('添加日志成功'))
})

/**
 * 删除日志
 */
router.delete('/log/:id', async (req, res) => {
  const id = req.params.id
  const info = await Log.findByIdAndDelete(id)
  assert(info, 400, '删除当前日志失败')
  res.json(new SuccessModel('删除日志成功'))
})

/**
 * 模拟数据
 */
router.get('/mockData', async (req, res) => {
  for (let i = 0; i < 10; i++) {
    let facility = new Facility({
      name: faker.lorem.word(),
      status: 200,
      type: faker.lorem.word(),
      lon: faker.address.longitude(),
      lat: faker.address.latitude()
    })
    for (let j = 0; j < 10; j++) {
      let log = new Log({
        facilityName: facility.name,
        status: 200,
        time: faker.time.recent(),
        event: faker.lorem.sentence()
      })
      facility.logs.push(log)
      await log.save()
    }
    await facility.save()
  }
  res.json({
    status: 200
  })
  // Facility.find().populate('logs')
})

module.exports = router
